package cn.HL.system.service.impl;

import cn.HL.org.domain.Department;
import cn.HL.system.domain.Menu;
import cn.HL.system.mapper.MenuMapper;
import cn.HL.system.service.IMenuService;
import cn.HL.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HL
 * @since 2022-06-21
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> tree() {
        List<Menu> menus = menuMapper.loadAll();
        Map<Long,Menu> allMenus= new HashMap<>();
        List<Menu> firstMenus= new ArrayList<>();

        for (Menu menu : menus) {
            allMenus.put(menu.getId(),menu);
        }
        for (Menu menu : menus) {
            if(menu.getParentId()==null){
                firstMenus.add(menu);
            }else{
                Menu ParentMenu = allMenus.get(menu.getParentId());
                if(ParentMenu!=null){
                    ParentMenu.getChildren().add(menu);
                }

            }
        }

        return firstMenus;



    }
}
